package com.syedu.cels.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.syedu.cels.dao.BaseDao;
import com.syedu.cels.entity.Vocabulary;
import com.syedu.cels.entity.Word;
import com.syedu.cels.service.BaseService;
import com.syedu.cels.util.BeanFactory;
import com.syedu.cels.util.InputUtil;
import com.syedu.cels.util.PageBean;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.*;

/**
 * description：业务接口的实现类
 */
public class BaseServiceImpl implements BaseService {

    BaseDao baseDao = (BaseDao) BeanFactory.getBean("baseDao");

    @Override
    public PageBean<Word> findWordsByPage(List<Word> words,int pageNow, int pageSize) throws Exception {
        List<Word> pageList = CollUtil.page(pageNow - 1, pageSize, words);
        PageBean<Word> pageBean = new PageBean<>(pageNow, pageSize);
        pageBean.setCounts(words.size());
        pageBean.setDatas(pageList);
        return pageBean;
    }

    @Override
    public PageBean<Vocabulary> findVocabulariesByPage(int pageNow, int pageSize) throws Exception {
        List<Vocabulary> vocabularies = baseDao.selectVocabularies();
        List<Vocabulary> pageList = CollUtil.page(pageNow - 1, pageSize, vocabularies);
        for (int i = 0; i < pageList.size(); i++) {
            pageList.get(i).setId((long) (pageNow - 1) * pageSize+ i + 1);
        }

        PageBean<Vocabulary> pageBean = new PageBean<>(pageNow, pageSize);
        pageBean.setCounts(vocabularies.size());
        pageBean.setDatas(pageList);
        return pageBean;
    }

    @Override
    public Map<Character, List<Word>> showWordsByMap() throws Exception {
        Map<Character, List<Word>> map = new HashMap<>();
        List<Word> words = baseDao.selectWords();

        for (Word w : words) {
            Character key = w.getFirst();

            if (map.containsKey(key)) {
                map.get(key).add(w);

            } else {
                List<Word> subWords = Lists.newArrayList(w);
                map.put(key, subWords);

            }
        }
        return map;
    }

    @Override
    public void showWordsMap(Map<Character, List<Word>> map){
        Set<Character> set = map.keySet();
        Iterator<Character> iterator = set.iterator();
        int count = 0;
        while(iterator.hasNext()){
            Character key = iterator.next();
            System.out.print(key + " [" + map.get(key).size() +"]\t\t\t");
            count++;
            if(count % 5 == 0){
                System.out.println();
            }
        }
        System.out.println("--------------------------------");
    }
    @Override
    public void en2cn(boolean isNew) throws Exception{
        if (isNew==false) {
            Map<String, String> map = new HashMap<>();
            List<Word> words = baseDao.selectWords();
            Collections.shuffle(words);
            for (Word w : words) {
                String key = w.getEn();
                map.put(key, w.getCn().toString());
            }
            long count = 0;
            long i = 0;
            for (Map.Entry<String, String> vo : map.entrySet()) {
                i++;
                vo.getKey();
                vo.getValue();
                System.out.print("英文：" + vo.getKey() + "翻译为：");
                String cn = InputUtil.getStr("");
                if (vo.getValue().contains(cn)) {
                    System.out.println("答对了");
                    count++;
                } else {
                    System.out.println("错了！！！ 正确翻译为：" + vo.getValue());
                }
                if (i == 10) {
                    System.out.println("本次得分：" + count + "正确率为：" + (count % i) * 10 + "%");
                    break;
                }
            }

        }
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("history\\en2cn.dic"));
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("history\\en2cn.dic"));
        HashMap map1 = (HashMap) objectInputStream.readObject();


    }

}